/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.gui;

import bobbietalk.Utilities;
import static bobbietalk.gui.ProgrammingBlock.p;
import bobbietalk.gui.blocks.ForwardBlock;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author roberto
 */
public class JigsawPuzzlePieceButton extends ShapedButton {

    private int iconSize = 96;
    private String iconName = null;

    public enum PieceType {

        LEFT, MIDDLE, RIGHT
    };
    private PieceType pieceType = PieceType.MIDDLE;
    protected static int b = 10; // usato nelle cubiche
    protected static int s = 5; // usato nelle cubiche
    protected static int d = 30;  // profondita dente
    protected static int hd2 = 15; // semi altezza dentello
    protected static int w = 170; // fine bordo dentellato (total width)
    protected static int h = 180; // altezza
    protected static int ds = 0; // differenza tra dentello maschio e femmina
    protected static int we = w / 4; // differenza in larghezza dei blocchi estremi
    protected static int h2 = h / 2; // semi altezza
    protected static int p = w - d; // inizio bordo dentellato

    public void JigsawPuzzlePieceButton() {

        buildPath();

    }

    private void buildPath() {

        GeneralPath path = new GeneralPath();

        switch (pieceType) {

            case LEFT:

                path.moveTo(0, 0);
                path.lineTo(p - we, 0);
                path.lineTo(p - we, (h2 - hd2));

                path.curveTo(p + b - we, (h2 - hd2) + b, w - b - we, (h2 - hd2) - b, w - we, (h2 - hd2));
                path.curveTo(w + s - we, (h2 - hd2) + s, w + s - we, (h2 + hd2) + s, w - we, (h2 + hd2));
                path.curveTo(w - b - we, (h2 + hd2) + b, p + b - we, (h2 + hd2) - b, p - we, (h2 + hd2));

                path.lineTo(p - we, (h2 + hd2));
                path.lineTo(p - we, h);
                path.lineTo(0, h);

                path.closePath();
                break;

            case MIDDLE:
                path.moveTo(0, 0);
                path.lineTo(p, 0);
                path.lineTo(p, (h2 - hd2));

                path.curveTo(p + b, (h2 - hd2) + b, w - b, (h2 - hd2) - b, w, (h2 - hd2));
                path.curveTo(w + s, (h2 - hd2) + s, w + s, (h2 + hd2) + s, w, (h2 + hd2));
                path.curveTo(w - b, (h2 + hd2) + b, p + b, (h2 + hd2) - b, p, (h2 + hd2));

                path.lineTo(p, (h2 + hd2));
                path.lineTo(p, h);
                path.lineTo(0, h);

                path.lineTo(0, (h2 + hd2 + ds));
                path.curveTo(0 + b, (h2 + hd2 + ds) - b, (d + ds) - b, (h2 + hd2 + ds) + b, (d + ds), (h2 + hd2 + ds));
                path.curveTo((d + ds) + s, (h2 + hd2 + ds) - s, (d + ds) + s, (h2 - hd2 + ds) + s, (d + ds), (h2 - hd2 + ds));
                path.curveTo((d + ds) - b, (h2 - hd2 + ds) - b, 0 + b, (h2 - hd2 + ds) + b, 0, (h2 - hd2 + ds));
//        path.lineTo(0, 0);
                path.closePath();
                break;

            case RIGHT:
                path.moveTo(0, 0);
                path.lineTo(p - we, 0);
                path.lineTo(p - we, (h2 - hd2));


                path.lineTo(p - we, (h2 + hd2));
                path.lineTo(p - we, h);
                path.lineTo(0, h);

                path.lineTo(0, (h2 + hd2 + ds));
                path.curveTo(0 + b, (h2 + hd2 + ds) - b, (d + ds) - b, (h2 + hd2 + ds) + b, (d + ds), (h2 + hd2 + ds));
                path.curveTo((d + ds) + s, (h2 + hd2 + ds) - s, (d + ds) + s, (h2 - hd2 + ds) + s, (d + ds), (h2 - hd2 + ds));
                path.curveTo((d + ds) - b, (h2 - hd2 + ds) - b, 0 + b, (h2 - hd2 + ds) + b, 0, (h2 - hd2 + ds));
//        path.lineTo(0, 0);

                path.closePath();
                break;

        }

        setShape(path);

    }

    public void setPieceType(PieceType type) {
        this.pieceType = type;
        buildPath();
    }

    public int getIconSize() {
        return iconSize;
    }

    public void setIconSize(int iconSize) {
        this.iconSize = iconSize;
    }

    public String getIconName() {
        return iconName;
    }

    public void setIconName(String iconName) {
        this.iconName = iconName;
        initIcon();
    }

    private void initIcon() {
        try {
            BufferedImage icon = ImageIO.read(getClass().getResourceAsStream(String.format("/icons/%d/%s.png", getIconSize(), getIconName())));
            setIcon(new ImageIcon(icon));
        } catch (IOException ex) {
            Utilities.getLogger().log(Level.SEVERE, null, ex);
        }
    }
}
